package com.cn.springcloudwechat.controller.find.DelicateAskAnswer;


import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.find.DelicateAskAnswer.YmDelicateArticle;
import com.cn.springcloudwechat.model.find.DelicateAskAnswer.YmDelicateAskAnswer;
import com.cn.springcloudwechat.service.find.DelicateAskAnswer.DelicateAskAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.HashMap;


/*
*精选问答controller
* */
@Controller
@ResponseBody
    @RequestMapping("/DeclicateAskAnswer/")
public class DelicateAskAnswerController {
    @Autowired
    private DelicateAskAnswerService delicateAskAnswerService;
    /**
     * 添加菜单
     * @param ymDelicateAskAnswer 菜单信息
     * return
     * */
    @RequestMapping("insertMenu")
    @LogInfo(operateType = OperateType.INSERT, content = "精选问答-添加菜单")
    public AjaxRes insertMenu(AjaxRes ajaxRes,YmDelicateAskAnswer ymDelicateAskAnswer){
        try{
            Integer flag=delicateAskAnswerService.addMenu(ymDelicateAskAnswer);
            ajaxRes.setSucceed(flag, Const.OPERATE_SUCCEED);
        }catch (Exception e ){
            ajaxRes.setFailMsg(e.getMessage());
        }

            return ajaxRes;
    }
    /*
     * 删除菜单，会连带删除子菜单和菜单的文章
     * @param ymDelicateMenuId 菜单id
     * return
     * */
    @RequestMapping("deleteMenu")
    @LogInfo(operateType = OperateType.DELETE, content = "精选问答-删除菜单")
    public AjaxRes deleteMenu(AjaxRes ajaxRes,@RequestParam("ymDelicateMenuId[]") Long[] ymDelicateMenuId,int state){
        try{
            Integer flag=delicateAskAnswerService.deleteMenu(ymDelicateMenuId,state);
            ajaxRes.setSucceed(flag,Const.DEL_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 查找菜单信息
     * @param ymDelicateMenuId 菜单id
     * return 菜单信息
     * */
    @RequestMapping("selectMenu")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询菜单")
    public AjaxRes selectMenu(AjaxRes ajaxRes,Long ymDelicateMenuId){
        try{
            YmDelicateAskAnswer ymDelicateAskAnswer=delicateAskAnswerService.selectMenu(ymDelicateMenuId);
            ajaxRes.setSucceed(ymDelicateAskAnswer,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /*
     * 查找一级菜单信息
     * @param
     * return  子菜单列表
     * */
    @RequestMapping("selectParentMenu")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询一级菜单")
    public AjaxRes selcetParentMenu(AjaxRes ajaxRes){
        try{
            ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswer=delicateAskAnswerService.selectParentMenu();
            ajaxRes.setSucceed(ymDelicateAskAnswer,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 查找2级菜单信息
     * @param
     * return  2菜单列表
     * */
    @RequestMapping("selectChildMenu")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询二级菜单")
    public AjaxRes selcetChildMenu(AjaxRes ajaxRes){
        try{
            ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswer=delicateAskAnswerService.selectChildMenu();
            ajaxRes.setSucceed(ymDelicateAskAnswer,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    @RequestMapping("queryChildMenuByParentId")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-通过父级菜单id查菜单")
    public AjaxRes queryChhildMenuByParentId(AjaxRes ajaxRes,Long parentId){
        try{
            ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswers=delicateAskAnswerService.queryChildMenuByParentId(parentId);
            ajaxRes.setSucceed(ymDelicateAskAnswers,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 添加文章
     * @param ymDelicateMenuId文章所属菜单id
     * @param ymDelicateArticle 文章信息
     * return
     * */
    @RequestMapping("insertArticle")
    @LogInfo(operateType = OperateType.INSERT, content = "精选问答-添加文章")
    public AjaxRes insertArticle(AjaxRes ajaxRes, Long ymDelicateMenuId,YmDelicateArticle ymDelicateArticle){
        try{
            Integer flag=delicateAskAnswerService.addArticle(ymDelicateMenuId,ymDelicateArticle);
            ajaxRes.setSucceed(flag,Const.OPERATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 删除文章
     * @param ymDelicateArticleId 文章id
     * return s
     * */
    @RequestMapping("deleteArticle")
    @LogInfo(operateType = OperateType.DELETE, content = "精选问答-删除文章")
    public AjaxRes deleteArticle(AjaxRes ajaxRes, @RequestParam("ymDelicateArticleId[]") ArrayList<Long> ymDelicateArticleId, @RequestParam("state") int state){
        try{
            Integer flag=delicateAskAnswerService.deleteArticle(ymDelicateArticleId, state);
            ajaxRes.setSucceed(flag,Const.DEL_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 查找文章
     * @param ymDelicateMenuId 文章id
     * return 文章信息
     * */
    @RequestMapping("selectArticle")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询文章")
    public AjaxRes selectArticle(AjaxRes ajaxRes,Long ymDelicateArticleId){
        try{
            YmDelicateArticle ymDelicateArticle=delicateAskAnswerService.selectArticle(ymDelicateArticleId);
            ajaxRes.setSucceed(ymDelicateArticle,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 查找3级菜单下的所有文章
     * @param ymDelicateMenuId 菜单id
     * return 文章列表
     * */
    @RequestMapping("selectArticleByMenuId")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询3级菜单下的文章")
    public AjaxRes selectArticleByMenuId(AjaxRes ajaxRes,Long ymDelicateMenuId){
        try{
            ArrayList<YmDelicateArticle> ymDelicateArticles=delicateAskAnswerService.selectArticleList(ymDelicateMenuId);
            ajaxRes.setSucceed(ymDelicateArticles,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
    /*
     * 查找1级菜单下的所有3级菜单
     * @param ymDelicateMenuId 菜单id
     * return 文章列表
     * */
        @RequestMapping("selectThreeMenuByParentId")
        @LogInfo(operateType = OperateType.QUERY, content = "精选问答-查询一级菜单下的所有文章")
    public AjaxRes selectArticleByParentId(AjaxRes ajaxRes,Long ymDelicateParentId){
        try{
            ArrayList<YmDelicateAskAnswer>Menus=delicateAskAnswerService.selectThreeMenuByParentId(ymDelicateParentId);
            ajaxRes.setSucceed(Menus,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 后台查询菜单列表
     * @param ajaxRes
     * @return
     */
    @RequestMapping("queryMenuAdmin")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-后台查询菜单列表")
    public AjaxRes queryMenuAdmin(AjaxRes ajaxRes,YmDelicateAskAnswer ymDelicateAskAnswer,Page<YmDelicateAskAnswer> page){
        try{
            Page<YmDelicateAskAnswer> ymDelicateAskAnswers=delicateAskAnswerService.queryMenuAdmin(ymDelicateAskAnswer,page);
          ajaxRes.setSucceed(ymDelicateAskAnswers,Const.OPERATE_SUCCEED);
        }catch ( Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 后台查所有文章
     * @param ajaxRes
     * @return
     */
    @RequestMapping("queryArticleAdmin")
//    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-后台查询文章列表")
    public AjaxRes queryArticleAdmin(AjaxRes ajaxRes, YmDelicateArticle ymDelicateArticle,Page<HashMap<String,Object>> page){
        try{
            Page<HashMap<String,Object>>ymDelicateArticles=delicateAskAnswerService.queryArticleAdmin(ymDelicateArticle,page);
            ajaxRes.setSucceed(ymDelicateArticles,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 后台查询某级菜单
     * @param ajaxRes
     * @param menuGrade
     * @return
     */
    @RequestMapping("queryParentMenuAdmin")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-后台查询某级菜单列表")
    public AjaxRes queryParentMenuAdmin(AjaxRes ajaxRes,int menuGrade){
        try{
            ArrayList<YmDelicateAskAnswer>ymDelicateAskAnswers=delicateAskAnswerService.queryParentMenuAdmin(menuGrade);
            ajaxRes.setSucceed(ymDelicateAskAnswers,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 更改文章
     * @param ajaxRes
     * @param ymDelicateArticle
     * @param ymDelicateMenuId
     * @return
     */
    @RequestMapping("updateArticle")
    @LogInfo(operateType = OperateType.UPDATE, content = "精选问答-更改文章")
    public AjaxRes updateArticle(AjaxRes ajaxRes,YmDelicateArticle ymDelicateArticle,Long ymDelicateMenuId){
        try{
             Integer i=delicateAskAnswerService.updateArticle(ymDelicateArticle,ymDelicateMenuId);
            ajaxRes.setSucceed(i,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 更改菜单
     * @param ajaxRes
     * @param ymDelicateAskAnswer
     * @return
     */
    @RequestMapping("updateMenu")
    @LogInfo(operateType = OperateType.UPDATE, content = "精选问答-更改菜单")
    public  AjaxRes updateMenu(AjaxRes ajaxRes,YmDelicateAskAnswer ymDelicateAskAnswer){
        try{
            Integer i=delicateAskAnswerService.updateMenu(ymDelicateAskAnswer);
            ajaxRes.setSucceed(i,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 通过父级菜单id查子级菜单
     * @param ajaxRes
     * @param parentId 父级菜单id
     * @return
     */
    @RequestMapping("queryChildMenu")
    @LogInfo(operateType = OperateType.QUERY, content = "精选问答-通过父级菜单id查子级菜单")
    public AjaxRes queryChildMenu(AjaxRes ajaxRes,Long parentId){
        try{
            ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswers=delicateAskAnswerService.queryChildMenuByParentId(parentId);
            ajaxRes.setSucceed(ymDelicateAskAnswers,Const.OPERATE_SUCCEED);
        }catch(Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
}
